Micron Document
`:top
An `!interface`! in the `F33f`_`[Java programming language`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Java_(programming_language)]`_`f is an `F33f`_`[abstract type`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Abstract_type]`_`f that is used to declare a behavior that `F33f`_`[classes`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Class_(computer_science)]`_`f must implement. They are similar to `F33f`_`[protocols`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Protocol_(object-oriented_programming)]`_`f. Interfaces are declared using the `B100`F9d9interface`f`b `F33f`_`[keyword`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Java_keywords]`_`f, and may only contain `F33f`_`[method signature`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Method_signature]`_`f and constant declarations (variable declarations that are declared to be both `B100`F9d9`F33f`_`[static`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Static_variable]`_`f`f`b and `B100`F9d9`F33f`_`[final`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Final_(Java)]`_`f`f`b). All methods of an Interface do not contain implementation (method bodies) as of all versions below Java 8. Starting with Java 8, `B100`F9d9default`f`b`:cite-ref-footnotebloch2018-1-0[`F5bf`_`[1`#cite-note-footnotebloch2018-1]`_`f] and `B100`F9d9static`f`b`:cite-ref-footnotebloch2018-1-1[`F5bf`_`[1`#cite-note-footnotebloch2018-1]`_`f] methods may have implementation in the `B100`F9d9interface`f`b definition.`:cite-ref-2[`F5bf`_`[2`#cite-note-2]`_`f] Then, in Java 9, `B100`F9d9private`f`b and `B100`F9d9private static`f`b methods were added. At present, a Java interface can have up to six different types.

Interfaces cannot be `F33f`_`[instantiated`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Instance_(computer_science)]`_`f, but rather are implemented. A class that implements an interface must implement all of the non-default methods described in the interface, or be an `F33f`_`[abstract class`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Abstract_class]`_`f. Object references in Java may be specified to be of an interface type; in each case, they must either be `F33f`_`[null`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Null_pointer]`_`f, or be bound to an object that implements the interface.

One benefit of using interfaces is that they simulate `F33f`_`[multiple inheritance`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Multiple_inheritance]`_`f. All classes in Java must have exactly one `F33f`_`[base class`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Base_class]`_`f, the only exception being `B100`F9d9java.lang.Object`f`b (the `F33f`_`[root class`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Top_type]`_`f of the Java `F33f`_`[type system`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Type_system]`_`f); `F33f`_`[multiple inheritance`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Multiple_inheritance]`_`f of classes is not allowed. However, an interface may inherit multiple interfaces and a class may implement multiple interfaces.

>>Contents

• `F0af`_`[Overview`#overview]`_`f
• `F0af`_`[Usage`#usage]`_`f
• `F0af`_`[Defining an interface`#defining-an-interface]`_`f
• `F0af`_`[Implementing interfaces in a class`#implementing-interfaces-in-a-class]`_`f
• `F0af`_`[Subinterfaces`#subinterfaces]`_`f
• `F0af`_`[Examples`#examples]`_`f
• `F0af`_`[See also`#see-also]`_`f
• `F0af`_`[Citations`#citations]`_`f
• `F0af`_`[References`#references]`_`f
• `F0af`_`[External links`#external-links]`_`f

-─

>>Overview

Interfaces are used to encode similarities which the classes of various types share, but do not necessarily constitute a class relationship. For instance, a `F33f`_`[human`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Human]`_`f and a `F33f`_`[parrot`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Parrot]`_`f can both `F33f`_`[whistle`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Whistle]`_`f; however, it would not make sense to represent `B100`F9d9Human`f`bs and `B100`F9d9Parrot`f`bs as subclasses of a `B100`F9d9Whistler`f`b class. Rather they most likely be subclasses of an `B100`F9d9Animal`f`b class (likely with intermediate classes), but both would implement the `B100`F9d9Whistler`f`b interface.

Another use of interfaces is being able to use an `F33f`_`[object`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Object_(computer_science)]`_`f without knowing its type of class, but rather only that it implements a certain interface. For instance, if one were annoyed by a whistling noise, one may not know whether it is a human or a parrot, because all that could be determined is that a whistler is whistling. The call `B100`F9d9whistler.whistle()`f`b will call the implemented method `B100`F9d9whistle`f`b of object `B100`F9d9whistler`f`b no matter what class it has, provided it implements `B100`F9d9Whistler`f`b. In a more practical example, a `F33f`_`[sorting algorithm`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Sorting_algorithm]`_`f may expect an object of type `B100`F9d9Comparable`f`b. Thus, without knowing the specific type, it knows that objects of that type can somehow be sorted.

For example:

`B100`F9d9interface Bounceable {`f`b
`B100`F9d9 double pi = 3.1415;`f`b
`B100`F9d9 void setBounce(); // Note the semicolon`f`b
`B100`F9d9 // Interface methods are public, abstract and never final.`f`b
`B100`F9d9 // Think of them as prototypes only; no implementations are allowed.`f`b
`B100`F9d9}`f`b

An interface:

• declares only method headers and public constants.
• cannot be instantiated.
• can be implemented by a class.`:cite-ref-footnotebloch2018-1-2[`F5bf`_`[1`#cite-note-footnotebloch2018-1]`_`f]
• cannot extend a class.
• can extend several other interfaces.`:cite-ref-footnotebloch2018-1-3[`F5bf`_`[1`#cite-note-footnotebloch2018-1]`_`f]

>>Usage

>>>Defining an interface

Interfaces are defined with the following syntax (compare to `F33f`_`[Java's class definition`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Class_(software)]`_`f):

`B100`F9d9[visibility] interface InterfaceName [extends other interfaces] {`f`b
`B100`F9d9 constant declarations`f`b
`B100`F9d9 abstract method declarations`f`b
`B100`F9d9 static method declarations`f`b
`B100`F9d9}`f`b

Example: public interface Interface1 extends Interface2;

The body of the interface contains `F33f`_`[abstract methods`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Method_(computer_programming)]`_`f, but since all methods in an interface are, by definition, abstract, the `B100`F9d9abstract`f`b keyword is not required. Since the interface specifies a set of exposed behaviors, all methods are implicitly `B100`F9d9public`f`b.

Thus, a simple interface may be

`B100`F9d9public interface Predator {`f`b
`B100`F9d9 boolean chasePrey(Prey p);`f`b
`B100`F9d9 void eatPrey(Prey p);`f`b
`B100`F9d9}`f`b

The member type declarations in an interface are implicitly static, final and public, but otherwise they can be any type of class or interface.`:cite-ref-3[`F5bf`_`[3`#cite-note-3]`_`f]

>>>Implementing interfaces in a class

The syntax for implementing an interface uses this formula:

`B100`F9d9... implements InterfaceName[, another interface, another, ...] ...`f`b

`F33f`_`[Classes`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Class_(software)]`_`f may implement an interface. For example:

`B100`F9d9public class Lion implements Predator {`f`b
`B100`F9d9`f`b
`B100`F9d9 @Override`f`b
`B100`F9d9 public boolean chasePrey(Prey p) {`f`b
`B100`F9d9 // Programming to chase prey p (specifically for a lion)`f`b
`B100`F9d9 }`f`b
`B100`F9d9`f`b
`B100`F9d9 @Override`f`b
`B100`F9d9 public void eatPrey(Prey p) {`f`b
`B100`F9d9 // Programming to eat prey p (specifically for a lion)`f`b
`B100`F9d9 }`f`b
`B100`F9d9}`f`b

If a class implements an interface and does not implement all its methods, it must be marked as `B100`F9d9abstract`f`b. If a class is abstract, one of its `F33f`_`[subclasses`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Subclass_(computer_science)]`_`f is expected to implement its unimplemented methods, though if any of the abstract class' subclasses do not implement all interface methods, the subclass itself must be marked again as `B100`F9d9abstract`f`b.

Classes can implement multiple interfaces:

`B100`F9d9public class Frog implements Predator, Prey { ... }`f`b

Interfaces can share common class methods:

`B100`F9d9class Animal implements LikesFood, LikesWater {`f`b
`B100`F9d9 boolean likes() { return true; }`f`b
`B100`F9d9}`f`b

However a given class cannot implement the same or a similar interface multiple times:

`B100`F9d9class Animal implements Shares<Boolean>, Shares<Integer> ...`f`b
`B100`F9d9// Error: repeated interface`f`b

Interfaces are commonly used in the Java language for `F33f`_`[callbacks`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Callback_(computer_science)]`_`f,`:cite-ref-4[`F5bf`_`[4`#cite-note-4]`_`f] as Java does not allow multiple inheritance of classes, nor does it allow the passing of methods (procedures) as arguments. Therefore, in order to pass a method as a parameter to a target method, current practice is to define and pass a reference to an interface as a means of supplying the signature and address of the parameter method to the target method rather than defining multiple variants of the target method to accommodate each possible calling class.

>>>Subinterfaces

Interfaces can extend several other interfaces, using the same formula as described below. For example,

`B100`F9d9public interface VenomousPredator extends Predator, Venomous {`f`b
`B100`F9d9 // Interface body`f`b
`B100`F9d9}`f`b

is legal and defines a subinterface. It allows multiple inheritance, unlike classes. `B100`F9d9Predator`f`b and `B100`F9d9Venomous`f`b may possibly define or inherit methods with the same signature, say `B100`F9d9kill(Prey p)`f`b. When a class implements `B100`F9d9VenomousPredator`f`b it will implement both methods simultaneously.

>>Examples

Some common `F33f`_`[Java`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Java_(Sun)]`_`f interfaces are:

• `B100`F9d9Comparable`f`b has the method `B100`F9d9compareTo`f`b, which is used to describe two objects as equal, or to indicate one is greater than the other. `F33f`_`[Generics`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Generic_programming]`_`f allow implementing classes to specify which class instances can be compared to them.
• `B100`F9d9Serializable`f`b is a `F33f`_`[marker interface`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Marker_interface]`_`f with no methods or fields - it has an empty body. It is used to indicate that a class can be `F33f`_`[serialized`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Serialization]`_`f. Its `F33f`_`[Javadoc`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Javadoc]`_`f describes how it should function, although nothing is programmatically enforced

>>See also

• `F33f`_`[Interface (object-oriented programming)`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Interface_(object-oriented_programming)]`_`f
• `F33f`_`[Mixin`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Mixin]`_`f
• `F33f`_`[Trait (computer programming)`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Trait_(computer_programming)]`_`f

>>Citations

`:cite-note-footnotebloch2018-1`!1.`! `F0af`_`[↑`#cite-ref-footnotebloch2018-1-0]`_`f `F33f`_`[Bloch 2018`#citerefbloch2018]`_`f.
`:cite-note-2`!2.`! `F0af`_`[↑`#cite-ref-2]`_`f "Default Methods". Archived from the original on 2017-05-23. Retrieved 2014-06-30.
`:cite-note-3`!3.`! `F0af`_`[↑`#cite-ref-3]`_`f "The Java Language Specification".
`:cite-note-4`!4.`! `F0af`_`[↑`#cite-ref-4]`_`f `:citerefmitchell1996`aMitchell, John D. (June 1, 1996). "Java Tip 10: Implement callback routines in Java". `*`F33f`_`[JavaWorld`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=JavaWorld]`_`f`*. Retrieved 2020-07-14.

>>References

• `:citerefbloch2018`aBloch, Joshua (2018). `*"Effective Java: Programming Language Guide"`* (third ed.). Addison-Wesley. `F33f`_`[ISBN`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=ISBN_(identifier)]`_`f 978-0134685991.

>>External links

• What Is an Interface?

`c`F0af`_`[↑ Back to top`#top]`_`f`a